package com.pybox.service;

import com.pybox.dto.TVersionDto;
import com.pybox.entity.TVersion;
import com.pybox.repository.TVersionRepository;
import com.pybox.spec.TVersionSpec;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wuyingkai on 2018/10/29.
 */
@Service
@Transactional
public class TVersionService {

        @Autowired
        private TVersionRepository repository;

        /**
         * 创建TSession
         *
         * @param source
         * @return
         */
        public boolean create(TVersionDto source){

            TVersion target = new TVersion();
            BeanUtils.copyProperties(source, target);
            repository.save(target);

            return true;

        }

        /**
         * 根据ID删除TSession
         *
         * @param id
         * @return
         */
        public boolean delete(Integer id){

            repository.delete(id);

            return true;

        }

        /**
         * 更新TSession
         *
         * @param id
         * @param source
         * @return
         */
        public boolean update(Integer id, TVersionDto source){

            TVersion exist = repository.findOne(id);
            if (null == exist){
                throw new RuntimeException("记录不存在");
            }

            BeanUtils.copyProperties(source, exist, new String[]{"id"});
            repository.save(exist);

            return true;

        }

        /**
         * 根据ID查询TSession
         *
         * @param id
         * @return
         */
        public TVersionDto detail(Integer id){

            TVersion exist = repository.findOne(id);
            if (null == exist){
                throw new RuntimeException("记录不存在");
            }

            TVersionDto target = new TVersionDto();
            BeanUtils.copyProperties(exist, target);

            return target;

        }

        /**
         * 分页查询查询TSession
         *
         * @param pageNo
         * @param pageSize
         * @param paramMap
         * @return
         */
        public Page<TVersionDto> page(Integer pageNo, Integer pageSize, Map<String, String> paramMap){

            Pageable pageable = new PageRequest(pageNo - 1, pageSize);
            Page<TVersion> data = repository.findAll(new TVersionSpec(paramMap), pageable);

            List<TVersionDto> list = new ArrayList<>();
            for (TVersion obj : data.getContent()) {
                TVersionDto dto = new TVersionDto();
                BeanUtils.copyProperties(obj, dto);
                list.add(dto);
            }

            Page<TVersionDto> page = new PageImpl<>(list, pageable, data.getTotalElements());

            return page;

        }

}
